package com.itguigu.auth.activiti;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 加班流程测试 - Candidate-users候选人
 */
@SpringBootTest
public class ProcessTestJiaban4 {

    @Autowired
    private RepositoryService repositoryService;


    /**
     * 1流程定义部署
     */
    @Test
    public void deployProcess() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/jiaban04.bpmn20.xml")
                .addClasspathResource("process/jiaban.png")
                .name("加班申请流程4")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }

    @Autowired
    private RuntimeService runtimeService;

    /**
     * 2开启流程实例
     */
    @Test
    public void startUpProcessInstance() {
        // Map<String, Object> variables = new HashMap<>();
        // variables.put("assignee1", "lusi02");
        // variables.put("assignee2", "mary02");
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("jiaban04");
        System.out.println("processInstance.getProcessDefinitionId() = " + processInstance.getProcessDefinitionId());
        System.out.println("processInstance.getId() = " + processInstance.getId());
    }

    @Autowired
    private TaskService taskService;

    /**
     * 3. 查询候选人
     */
    @Test
    public void findGroupTaskList() {
        List<Task> list = taskService.createTaskQuery()
                .taskCandidateUser("tangmu01")
                .list();
        for (Task task : list) {
            System.out.println("task.getProcessDefinitionId() = " + task.getProcessDefinitionId());
            System.out.println("task.getName() = " + task.getName());
            System.out.println("task.getId() = " + task.getId());
            System.out.println("task.getAssignee() = " + task.getAssignee());
        }
    }

    /**
     * 4. 拾取组任务
     */
    @Test
    public void claimTask() {
        Task task = taskService.createTaskQuery()
                .taskCandidateUser("tangmu01")
                .singleResult();
        if (task != null) {
            taskService.claim(task.getId(), "tangmu01");
            System.out.println("任务拾取完成");
        }
    }

    /**
     * 5. 查询代办任务
     */
    @Test
    public void findTaskList() {
        List<Task> list = taskService.createTaskQuery()
                .taskAssignee("tangmu01")
                .list();
        for (Task task : list) {
            System.out.println("task.getProcessDefinitionId() = " + task.getProcessDefinitionId());
            System.out.println("task.getName() = " + task.getName());
            System.out.println("task.getId() = " + task.getId());
            System.out.println("task.getAssignee() = " + task.getAssignee());
        }
    }


    /**
     * 6. 办理任务
     */
    @Test
    public void complateTask() {
        Task task = taskService.createTaskQuery()
                .taskAssignee("tangmu01")
                .singleResult();

        taskService.complete(task.getId());
    }

    /**
     * 归还组任务
     */
    @Test
    public void assigneeToGroupTask() {
        String taskId = "d96c3f28-825e-11ed-95b4-7c57581a7819";
        // 任务负责人
        String userId = "zhangsan01";
        // 校验userId是否是taskId的负责人，如果是负责人才可以归还组任务
        Task task = taskService
                .createTaskQuery()
                .taskId(taskId)
                .taskAssignee(userId)
                .singleResult();
        if (task != null) {
            // 如果设置为null，归还组任务,该 任务没有负责人
            taskService.setAssignee(taskId, null);
        }
    }


    /**
     * 任务交接
     */
    @Test
    public void assigneeToCandidateUser() {
        // 当前待办任务
        String taskId = "d96c3f28-825e-11ed-95b4-7c57581a7819";
        // 校验zhangsan01是否是taskId的负责人，如果是负责人才可以归还组任务
        Task task = taskService
                .createTaskQuery()
                .taskId(taskId)
                .taskAssignee("zhangsan01")
                .singleResult();
        if (task != null) {
            // 将此任务交给其它候选人zhangsan02办理该 任务
            taskService.setAssignee(taskId, "zhangsan02");
        }
    }

}
